package com.buba.customer.controller;

import com.buba.customer.pojo.Customer;
import com.buba.customer.service.CustomerService;
import com.buba.system.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/Customer")
@RestController
public class CustomerController {
    private CustomerService customerService;

    //添加方法
    @RequestMapping("/addCustomer")
    public Map<String, Object> addCustomer(@RequestParam Map<String, Object> map){
        //获取时间戳
        Long sj= System.currentTimeMillis();
        String a = sj+"";
        map.put("customerTime",a);
        Integer count=customerService.addCustomer(map);
        if(count==1){
            map.put("message","添加成功");
        }else{
            map.put("message","添加失败");
        }
        return map;
    }
    //分页查询
    @RequestMapping("/selCustomer")
    public Page selCustomer(@RequestParam Map<String, Object> map,String companyName){
        Page page = new Page((Integer.parseInt(map.get("pageNow").toString())),(Integer.parseInt(map.get("pageCount").toString())));
        map.put("page", page);
        map.put("companyName", companyName);
        List<Customer> list = null;
        if (companyName==null || companyName=="") {
            //查询客户总条数
            page.computePageTotal(customerService.selCustomerCount());
            //查询所有客户信息
            list = customerService.selCustomer(page);
        } else { // 携带查询条件
            //查询客户总条数
            page.computePageTotal(customerService.selCustomerCountByName(map));
            //查询所有客户信息
            list = customerService.selCustomerByName(map);
        }
        page.setCustomerList(list);
        return page;
    }

    //批量删除
    @RequestMapping("/delCustomerAll")
    public Map<String,Object> delCustomerAll(String ids){
        Map<String,Object> map=new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer count=customerService.delCustomerAll(list);
        if(count!=null){
            map.put("message","删除"+count+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }

    @RequestMapping("/updCustomer")
    public Map<String,Object> updCustomer(@RequestParam Map<String, Object> map){
        Integer count=customerService.updCustomer(map);
        if(count!=null){
            map.put("message","修改成功");
        }else{
            map.put("message","修改失败");
        }
        return map;
    }
    @RequestMapping("/selCustomerNo")
    public Map<String,Object> selCustomerNo(){
        Map<String,Object> map = new HashMap<>();
        List<Customer> list=customerService.selCustomerNo();
        map.put("list",list);
        return map;
    }
    @Autowired
    @Qualifier("customerServiceImpl")
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}
